package com.example.blackbox.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.blackbox.Adapter.DialogAdapter;
import com.example.blackbox.R;

import java.io.ByteArrayInputStream;

public class SettingActivity extends Activity {
    SharedPreferences sp;
    TextView name;
    ImageView head;
    Button exit;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initUI();
        String names=sp.getString("name",null);
        String img=sp.getString("img",null);

        name.setText(names);
        try{
            byte[] byteArray= Base64.decode(img, Base64.DEFAULT);
            if(byteArray.length!=0){
                ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
                Bitmap bitmap= BitmapFactory.decodeStream(byteArrayInputStream);
                head.setImageBitmap(bitmap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final DialogAdapter dialogAdapter=new DialogAdapter(SettingActivity.this,R.style.Dialog);
                dialogAdapter.setYesOnclickListener("确定", new DialogAdapter.onYesOnclickListener() {
                    @Override
                    public void onYesOnclick() {
                        Intent intent=new Intent();
                        intent.setAction("EXIT_INFO_ACTION");
                        sendBroadcast(intent);
                        startActivity(new Intent(SettingActivity.this,LoginActivity.class));
                        finish();
                    }
                });
                dialogAdapter.setNoOnclickListener("取消", new DialogAdapter.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        dialogAdapter.dismiss();
                    }
                });
                dialogAdapter.show();

            }
        });

    }

    public void initUI(){
        name=(TextView)findViewById(R.id.setting_tv_name);
        head=(ImageView)findViewById(R.id.setting_iv_head);
        exit=(Button)findViewById(R.id.setting_bt_exit);
        sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE);    }
}
